package com;

import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.secure.SaBase64Util;
import cn.dev33.satoken.secure.SaSecureUtil;

import java.util.HashMap;

/**
 * @Author sc
 * @DATE 2023/11/10 11:01
 * @Description:
 * @Version 1.0
 */
public class SecoreController {
    public static void main(String[] args) throws Exception {
        //md5加密
        String md5 = SaSecureUtil.md5("123456");
        //md5+盐
        String md5BySalt = SaSecureUtil.md5BySalt("123456", "abc");
        //sha1加密
        String sha1 = SaSecureUtil.sha1("123456");
        //sha256加密
        String sha256 = SaSecureUtil.sha256("123456");
        //对称加密
//        定义密钥和明文
        String key="123456";
        String text="我是明文";
        //加密
        String aesEncrypt = SaSecureUtil.aesEncrypt(key, text);
        System.out.println("AES加密后："+aesEncrypt);
        //解密
        String aesDecrypt = SaSecureUtil.aesDecrypt(key, aesEncrypt);
        System.out.println("AES解密后："+aesDecrypt);
        System.out.println(md5);
        System.out.println(md5BySalt);
        System.out.println(sha1);
        System.out.println(sha256);
//        非对称加密 RSA加密
        //定义私钥和公钥
        // 生成一对公钥和私钥，其中Map对象 (private=私钥, public=公钥)
        HashMap<String, String> generateKeyPair = SaSecureUtil.rsaGenerateKeyPair();
        System.out.println(generateKeyPair);
        String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAO+wmt01pwm9lHMdq7A8gkEigk0XKMfjv+4IjAFhWCSiTeP7dtlnceFJbkWxvbc7Qo3fCOpwmfcskwUc3VSgyiJkNJDs9ivPbvlt8IU2bZ+PBDxYxSCJFrgouVOpAr8ar/b6gNuYTi1vt3FkGtSjACFb002/68RKUTye8/tdcVilAgMBAAECgYA1COmrSqTUJeuD8Su9ChZ0HROhxR8T45PjMmbwIz7ilDsR1+E7R4VOKPZKW4Kz2VvnklMhtJqMs4MwXWunvxAaUFzQTTg2Fu/WU8Y9ha14OaWZABfChMZlpkmpJW9arKmI22ZuxCEsFGxghTiJQ3tK8npj5IZq5vk+6mFHQ6aJAQJBAPghz91Dpuj+0bOUfOUmzi22obWCBncAD/0CqCLnJlpfOoa9bOcXSusGuSPuKy5KiGyblHMgKI6bq7gcM2DWrGUCQQD3SkOcmia2s/6i7DUEzMKaB0bkkX4Ela/xrfV+A3GzTPv9bIBamu0VIHznuiZbeNeyw7sVo4/GTItq/zn2QJdBAkEA8xHsVoyXTVeShaDIWJKTFyT5dJ1TR++/udKIcuiNIap34tZdgGPI+EM1yoTduBM7YWlnGwA9urW0mj7F9e9WIQJAFjxqSfmeg40512KP/ed/lCQVXtYqU7U2BfBTg8pBfhLtEcOg4wTNTroGITwe2NjL5HovJ2n2sqkNXEio6Ji0QQJAFLW1Kt80qypMqot+mHhS+0KfdOpaKeMWMSR4Ij5VfE63WzETEeWAMQESxzhavN1WOTb3/p6icgcVbgPQBaWhGg==";
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDvsJrdNacJvZRzHauwPIJBIoJNFyjH47/uCIwBYVgkok3j+3bZZ3HhSW5Fsb23O0KN3wjqcJn3LJMFHN1UoMoiZDSQ7PYrz275bfCFNm2fjwQ8WMUgiRa4KLlTqQK/Gq/2+oDbmE4tb7dxZBrUowAhW9NNv+vESlE8nvP7XXFYpQIDAQAB";
        //使用公钥加密
        String publicKeys = SaSecureUtil.rsaEncryptByPublic(publicKey, "我是私钥");
        System.out.println("公钥加密后："+publicKeys);
        //使用私钥解密
        String aPrivate = SaSecureUtil.rsaDecryptByPrivate(privateKey, publicKeys);
        System.out.println("私钥解密后："+aPrivate);
        //Base64编码与解码
        String text2="我是Base";
        //使用Base64编码
        String encode = SaBase64Util.encode(text2);
        System.out.println("Base编码后："+encode);
        //使用Base64解码
        String decode = SaBase64Util.decode(encode);
        System.out.println("Base解码后："+decode);
        //BCrypt加密
        //由它加密的文件可在所有支持的操作系统和处理器上进行转移
        //它的口令必须是8至56个字符，并将在内部被转化为448位的密钥
        String hashpw = BCrypt.hashpw("123456", BCrypt.gensalt());
        // 使用checkpw方法检查被加密的字符串是否与原始字符串匹配：
        boolean checkpw = BCrypt.checkpw(hashpw, "123456");
        // gensalt方法提供了可选参数 (log_rounds) 来定义加盐多少，也决定了加密的复杂度:
        String strong_salt = BCrypt.gensalt(10);
        String stronger_salt = BCrypt.gensalt(12);
        System.out.println(checkpw);
        System.out.println(hashpw);
    }
}
